package com.ktjy.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktjy.goods.domain.Goods;
import com.ktjy.goods.domain.TGoodsAttrValues;
import com.ktjy.goods.domain.TGoodsInfo;
import com.ktjy.goods.mapper.TGoodsAttrValuesMapper;
import com.ktjy.goods.mapper.TGoodsInfoMapper;
import com.ktjy.goods.service.ITGoodsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-15
 */
@Service
public class TGoodsInfoServiceImpl extends ServiceImpl<TGoodsInfoMapper, TGoodsInfo> implements ITGoodsInfoService {

    @Autowired
    private TGoodsInfoMapper tGoodsInfoMapper;

    @Autowired
    private TGoodsAttrValuesMapper tGoodsAttrValuesMapper;

    @Override
    public List<TGoodsInfo> selectGoodsList(TGoodsInfo tGoodsInfo) {
        return tGoodsInfoMapper.selectGoodsList(tGoodsInfo);
    }

    @Override
    public TGoodsInfo getGoodsInfoById(Long id) {
        return tGoodsInfoMapper.getGoodsInfoById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteGoodsAndAttrValues(List<Integer> goodsIds) {
        try {
            // 删除 TGoodsAttrValues 表中符合条件的记录
            QueryWrapper<TGoodsAttrValues> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("goods_id", goodsIds);
            tGoodsAttrValuesMapper.delete(queryWrapper);

            QueryWrapper<TGoodsInfo> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("id", goodsIds);

            // 删除 TGoodsInfo 表中对应的记录
            return tGoodsInfoMapper.delete(queryWrapper1) >= goodsIds.size();
        } catch (Exception e) {
            // 若出现异常，事务会自动回滚
            throw e;
        }
    }
}
